package org.ydsw.data.manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ydsw.data.manage.entity.PageVo;
import org.ydsw.data.manage.entity.ScheduleJobEntity;
import org.ydsw.data.manage.service.ScheduleJobService;
import org.ydsw.data.manage.utils.JsonResponse;

/**
 * 任务调度控制类
 * @author admin
 */
@RestController
@RequestMapping("/api/v1/platform/scheduleJob")
public class ScheduleJobController {

    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 分页查询数据清除策略数据
     * @param pageNum
     * @param pageSize
     * @param scheduleJobEntity
     * @return
     */
    @RequestMapping("queryPage")
    public JsonResponse queryPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                  @ModelAttribute ScheduleJobEntity scheduleJobEntity) {
        PageVo<ScheduleJobEntity> scheduleJobEntityList = scheduleJobService.queryPage(pageNum, pageSize, scheduleJobEntity);
        return JsonResponse.respSuccess("分页查询数据清除策略成功", scheduleJobEntityList);
    }

    /**
     * 更新数据清除策略数据
     * @param scheduleJobEntity
     * @return
     */
    @PostMapping("updateScheduleJobEntity")
    public JsonResponse updateScheduleJobEntity(@RequestBody ScheduleJobEntity scheduleJobEntity) {
        scheduleJobService.updateScheduleJobEntity(scheduleJobEntity);
        return JsonResponse.respSuccess("更新数据清除策略成功");
    }

    /**
     * 保存任务
     * @param scheduleJob
     */
    @PostMapping("saveTask")
    public JsonResponse saveTask(@RequestBody ScheduleJobEntity scheduleJob) {
        scheduleJobService.save(scheduleJob);
        return JsonResponse.respSuccess("保存任务成功");
    }

    /**
     * 更新任务
     * @param scheduleJob
     */
    @PostMapping("updateTask")
    public JsonResponse updateTask(@RequestBody ScheduleJobEntity scheduleJob) {
        scheduleJobService.update(scheduleJob);
        return JsonResponse.respSuccess("更新任务成功");
    }

    /**
     * 执行任务
     * @param jobIds
     */
    @PostMapping("runTask")
    public JsonResponse runTask(@RequestParam("jobIds") Long[] jobIds) {
        scheduleJobService.run(jobIds);
        return JsonResponse.respSuccess("执行任务成功");
    }

    /**
     * 暂停任务
     * @param jobIds
     */
    @PostMapping("pauseTask")
    public JsonResponse pauseTasks(@RequestParam("jobIds") Long[] jobIds) {
        scheduleJobService.pause(jobIds);
        return JsonResponse.respSuccess("暂停任务成功");
    }

    /**
     * 恢复任务
     * @param jobIds
     */
    @PostMapping("resumeTask")
    public JsonResponse resumeTask(@RequestParam("jobIds") Long[] jobIds) {
        scheduleJobService.resume(jobIds);
        return JsonResponse.respSuccess("恢复任务成功");
    }

}
